package com.macro.mall.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.*;
import com.macro.mall.model.OmsOrder;
import com.macro.mall.service.OmsOrderService;

import com.macro.mall.vo.HomeOrderInfoVo;
import com.macro.mall.vo.OmsOrderDayTotalVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

/**
 * 订单管理Controller
 * Created by macro on 2018/10/11.
 */
@Controller
@Tag(name = "OmsOrderController", description = "订单管理")
@RequestMapping("/order")
public class OmsOrderController {
    @Autowired
    private OmsOrderService orderService;

    /**
     * 首页订单信息
     */
    @RequestMapping(value = "/getHomeOrderInfo", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<HomeOrderInfoVo> getHomeOrderInfo() {
        HomeOrderInfoVo homeOrderInfoVo = new HomeOrderInfoVo();
        //获取当天时间
        DateTime today = DateUtil.parse(DateUtil.now());
        //当天的开始时间和结束时间
        Date beginOfDay = DateUtil.beginOfDay(today);
        Date endOfDay = DateUtil.endOfDay(today);
        OmsOrderQueryParam queryParamPub = new OmsOrderQueryParam();
        queryParamPub.setStartTime(beginOfDay.getTime());
        queryParamPub.setEndTime(endOfDay.getTime());
        //获取今天全部订单及金额
        CompletableFuture todayDto = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            queryParam.setStatus("123");
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);
            //设置今天全部订单及金额
            homeOrderInfoVo.setOrderTotalNumToday(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPayToday(homeOrderInfo.getOrderTotalPay());

        });
        //获取今天未支付订单及金额
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture todayNoPayDto = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            queryParam.setStatus("0");
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);
            //设置今天未支付订单及金额
            homeOrderInfoVo.setOrderTotalNumTodayNoPay(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPayTodayNoPay(homeOrderInfo.getOrderTotalPay());

        });
        //获取昨天支付订单及金额
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture yesterday = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            queryParam.setStatus("123");
            queryParam.setStartTime(beginOfDay.getTime() - 24 * 60 * 60 * 1000);
            queryParam.setEndTime(beginOfDay.getTime());
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);
            //设置昨天支付订单及金额
            homeOrderInfoVo.setOrderTotalNumYesterdayPay(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPayYesterdayPay(homeOrderInfo.getOrderTotalPay());

        });
        //获取7天支付订单及金额
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture day7 = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            queryParam.setStatus("123");
            queryParam.setStartTime(beginOfDay.getTime() - 7 * 24 * 60 * 60 * 1000);
            queryParam.setEndTime(beginOfDay.getTime());
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);

            //设置7天支付订单及金额
            homeOrderInfoVo.setOrderTotalNum7dayPay(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPay7dayPay(homeOrderInfo.getOrderTotalPay());
        });

        //获取本周支付订单及金额
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture week = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            DateTime beginOfWeek = DateUtil.beginOfWeek(today);
            DateTime endOftWeek = DateUtil.endOfWeek(today);
            queryParam.setStatus("123");
            queryParam.setStartTime(beginOfWeek.getTime());
            queryParam.setEndTime(endOftWeek.getTime());
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);

            //获取本周支付订单及金额
            homeOrderInfoVo.setOrderTotalNumWeekPay(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPayWeekPay(homeOrderInfo.getOrderTotalPay());
        });
        //获取上周支付订单及金额
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture weekLast = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            DateTime lastWeekDateTime = DateUtil.lastWeek();
            DateTime beginOfLastWeek = DateUtil.beginOfWeek(lastWeekDateTime.toJdkDate());
            DateTime endOfLastWeek = DateUtil.endOfWeek(lastWeekDateTime.toJdkDate());
            queryParam.setStatus("123");
            queryParam.setStartTime(beginOfLastWeek.getTime());
            queryParam.setEndTime(endOfLastWeek.getTime());
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);

            //获取上周支付订单及金额
            homeOrderInfoVo.setOrderTotalNumLastWeekPay(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPayLastWeekPay(homeOrderInfo.getOrderTotalPay());
        });
        //获取本月支付订单及金额
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture month = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            DateTime beginOfMonth = DateUtil.beginOfMonth(today);
            DateTime endOfMonth = DateUtil.endOfMonth(today);
            queryParam.setStatus("123");
            queryParam.setStartTime(beginOfMonth.getTime());
            queryParam.setEndTime(endOfMonth.getTime());
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);

            //设置本月支付订单及金额
            homeOrderInfoVo.setOrderTotalNumMonthPay(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPayMonthPay(homeOrderInfo.getOrderTotalPay());
        });
        //获取上月支付订单及金额
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture lastMonth = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            DateTime lastMonthDateTime = DateUtil.lastMonth();
            DateTime beginOfLastMonth = DateUtil.beginOfMonth(lastMonthDateTime.toJdkDate());
            DateTime endOfLastMonth = DateUtil.endOfMonth(lastMonthDateTime.toJdkDate());
            queryParam.setStatus("123");
            queryParam.setStartTime(beginOfLastMonth.getTime());
            queryParam.setEndTime(endOfLastMonth.getTime());
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);

            //获取上月支付订单及金额
            homeOrderInfoVo.setOrderTotalNumLasMonthPay(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPayLastMonthPay(homeOrderInfo.getOrderTotalPay());
        });
        //获取本月退货无效订单及金额
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture monthTuiHuo = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            BeanUtils.copyProperties(queryParamPub, queryParam);
            DateTime beginOfMonth = DateUtil.beginOfMonth(today);
            DateTime endOfMonth = DateUtil.endOfMonth(today);
            queryParam.setStatus("5");
            queryParam.setStartTime(beginOfMonth.getTime());
            queryParam.setEndTime(endOfMonth.getTime());
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);
            //设置本月退货无效订单及金额
            homeOrderInfoVo.setOrderTotalNumMonthTuiHuo(homeOrderInfo.getOrderTotalNum());
            homeOrderInfoVo.setOrderTotalPayMonthTuiHuo(homeOrderInfo.getOrderTotalPay());

        });

        //待付款订单
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture noPayOrder = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            queryParam.setStatus("0");
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);
            //待付款订单
            homeOrderInfoVo.setNoPayOrders(homeOrderInfo.getOrderTotalNum());

        });

        //待发货订单
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture toBeShippedOrders = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            queryParam.setStatus("1");
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);
            //待发货订单
            homeOrderInfoVo.setToBeShippedOrders(homeOrderInfo.getOrderTotalNum());

        });

        //已发货订单
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture shippedOrders = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            queryParam.setStatus("2");
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);
            //已发货订单
            homeOrderInfoVo.setShippedOrders(homeOrderInfo.getOrderTotalNum());

        });

        //已完成订单
        //"订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单"
        CompletableFuture completionOrders = CompletableFuture.runAsync(() -> {
            OmsOrderQueryParam queryParam = new OmsOrderQueryParam();
            queryParam.setStatus("3");
            HomeOrderInfoDto homeOrderInfo = orderService.getHomeOrderInfo(queryParam);
            //已完成订单
            homeOrderInfoVo.setCompletionOrders(homeOrderInfo.getOrderTotalNum());

        });

        todayDto.join();
        todayNoPayDto.join();
        yesterday.join();
        day7.join();
        week.join();
        weekLast.join();
        month.join();
        lastMonth.join();
        monthTuiHuo.join();
        noPayOrder.join();
        toBeShippedOrders.join();
        shippedOrders.join();
        completionOrders.join();

        homeOrderInfoVo.setOrderTotalNumWeekPayRatio(homeOrderInfoVo.getOrderTotalNumLastWeekPay() == 0 ? "暴涨" : (homeOrderInfoVo.getOrderTotalNumWeekPay() - homeOrderInfoVo.getOrderTotalNumLastWeekPay()>0?"+":"")+NumberUtil.div(homeOrderInfoVo.getOrderTotalNumWeekPay() - homeOrderInfoVo.getOrderTotalNumLastWeekPay(), homeOrderInfoVo.getOrderTotalNumLastWeekPay(),2)*100);
        homeOrderInfoVo.setOrderTotalPayWeekPayRatio(homeOrderInfoVo.getOrderTotalNumLastWeekPay() == 0 ? "暴涨" :  (homeOrderInfoVo.getOrderTotalPayWeekPay() - homeOrderInfoVo.getOrderTotalPayLastWeekPay()>0?"+":"")+NumberUtil.div(homeOrderInfoVo.getOrderTotalPayWeekPay() - homeOrderInfoVo.getOrderTotalPayLastWeekPay(), homeOrderInfoVo.getOrderTotalPayLastWeekPay(),2)*100);
        homeOrderInfoVo.setOrderTotalNumMonthPayRatio(homeOrderInfoVo.getOrderTotalNumLasMonthPay() == 0 ? "暴涨" : (homeOrderInfoVo.getOrderTotalNumMonthPay() - homeOrderInfoVo.getOrderTotalNumLasMonthPay()>0?"+":"")+ NumberUtil.div(homeOrderInfoVo.getOrderTotalNumMonthPay() - homeOrderInfoVo.getOrderTotalNumLasMonthPay(), homeOrderInfoVo.getOrderTotalNumLasMonthPay(),2*100));
        homeOrderInfoVo.setOrderTotalPayMonthPayRatio(homeOrderInfoVo.getOrderTotalPayLastMonthPay() == 0 ? "暴涨" : (homeOrderInfoVo.getOrderTotalPayMonthPay() - homeOrderInfoVo.getOrderTotalPayLastMonthPay()>0?"+":"")+ NumberUtil.div(homeOrderInfoVo.getOrderTotalPayMonthPay() - homeOrderInfoVo.getOrderTotalPayLastMonthPay(), homeOrderInfoVo.getOrderTotalPayLastMonthPay(),2*100));


        return CommonResult.success(homeOrderInfoVo);


    }

    @Operation(summary = "查询订单")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<OmsOrder>> list(OmsOrderQueryParam queryParam,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<OmsOrder> orderList = orderService.list(queryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(orderList));
    }

    @Operation(summary = "查询订单,每天销售统计")
    @RequestMapping(value = "/listDayTotal", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<OmsOrderDayTotalVo>> getListTotal(OmsOrderTotalQueryParamDto queryParam) {
        queryParam.setStatus("123");
        /*queryParam.setStartTime("2020-01-01");
        queryParam.setEndTime("2024-10-01");*/


        List<OmsOrderDayTotalVo> orderList = orderService.getListTotal(queryParam);
        return CommonResult.success(orderList);
    }

    @Operation(summary = "批量发货")
    @RequestMapping(value = "/update/delivery", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delivery(@RequestBody List<OmsOrderDeliveryParam> deliveryParamList) {
        int count = orderService.delivery(deliveryParamList);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Operation(summary = "批量关闭订单")
    @RequestMapping(value = "/update/close", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult close(@RequestParam("ids") List<String> ids, @RequestParam String note) {
        int count = orderService.close(ids, note);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Operation(summary = "批量删除订单")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<String> ids) {
        int count = orderService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Operation(summary = "获取订单详情:订单信息、商品信息、操作记录")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<OmsOrderDetail> detail(@PathVariable String id) {
        OmsOrderDetail orderDetailResult = orderService.detail(id);
        return CommonResult.success(orderDetailResult);
    }

    @Operation(summary = "修改收货人信息")
    @RequestMapping(value = "/update/receiverInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateReceiverInfo(@RequestBody OmsReceiverInfoParam receiverInfoParam) {
        int count = orderService.updateReceiverInfo(receiverInfoParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Operation(summary = "修改订单费用信息")
    @RequestMapping(value = "/update/moneyInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateReceiverInfo(@RequestBody OmsMoneyInfoParam moneyInfoParam) {
        int count = orderService.updateMoneyInfo(moneyInfoParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Operation(summary = "备注订单")
    @RequestMapping(value = "/update/note", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateNote(@RequestParam("id") String id,
                                   @RequestParam("note") String note,
                                   @RequestParam("status") Integer status) {
        int count = orderService.updateNote(id, note, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
